package com.lcxw.hxzbapi.controller.unioncontroller;

import com.lcxw.hxzbapi.entity.requestBean.DealUnionMemberApplyReq;
import com.lcxw.hxzbapi.entity.requestBean.DealUnionMemberIncomeReq;
import com.lcxw.hxzbapi.entity.requestBean.DeleteUnionMemberReq;
import com.lcxw.hxzbapi.entity.requestBean.GetUnionIncomeReq;
import com.lcxw.hxzbapi.entity.requestBean.GetUnionInfoReq;
import com.lcxw.hxzbapi.entity.requestBean.GetUnionMemIncomeReq;
import com.lcxw.hxzbapi.entity.requestBean.LoginUnionReq;
import com.lcxw.hxzbapi.entity.requestBean.RegistUnionReq;
import com.lcxw.hxzbapi.entity.requestBean.RequestParamBasic;
import com.lcxw.hxzbapi.entity.requestBean.ResetPasswordReq;
import com.lcxw.hxzbapi.entity.requestBean.ResetUnionInfoReq;
import com.lcxw.hxzbapi.entity.requestBean.SetUnionInfoReq;
import com.lcxw.hxzbapi.entity.requestBean.GetUnionMembersReq;
import com.lcxw.hxzbapi.entity.requestBean.getJoinListReq;
import com.lcxw.hxzbapi.entity.requestBean.getTurnInListReq;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/union")
@Slf4j
public class UnionWebController {

    @Resource
    UnionMemberRealization unionMemberRealization;

    @Resource
    UnionRealization unionRealization;

    //工会注册申请
    @PostMapping("/regist")
    public Response registUnion(@RequestBody RegistUnionReq registUnionReq) {
        return unionRealization.registUnion(registUnionReq);
    }


    //登录工会后台接口
    @PostMapping("/login")
    public Response loginUnion(@Validated @RequestBody LoginUnionReq loginUnionReq) {
        String telephone = loginUnionReq.getTelephone();
        String password = loginUnionReq.getPassword();
        try {
            return unionMemberRealization.login(telephone, password);
        } catch (Exception e) {
            return Response.dbFail(log, "数据库操作失败");
        }

    }

    //工会信息设置接口
    @PostMapping("/info/set")
    public Response setUnionInfo(@RequestBody SetUnionInfoReq setUnionInfoReq) {
        return unionMemberRealization.setUnionInfo(setUnionInfoReq);
    }

    //工会图标上传接口
    @PostMapping("/pic/upload")
    public Response picUpload(@RequestParam("file") MultipartFile file,
        @RequestParam("unionId") int unionId) {

        try {
            return Response.ok(unionMemberRealization.uploadUnionPic(file, unionId));
        } catch (Exception e) {
            return Response.error(ErrorCode.EXTERNAL_ERROR, "上传图片到服务器失败，请重新上传");
        }
    }

    //工会签约人员查询
    @PostMapping("/member/get")
    public Response getUnionMembers(@RequestBody GetUnionMembersReq getUnionMembersReq) {
        try {
            return unionMemberRealization.getUnionMembers(getUnionMembersReq);
        } catch (Exception e) {
            return Response.dbFail(log, "数据库操作失败");
        }
    }

    //处理加入、退出工会申请
    @PostMapping("/member/apply/deal")
    public Response dealUnionApply(@RequestBody DealUnionMemberApplyReq dealUnionMemberApplyReq) {
        try {
            return unionMemberRealization.dealUnionMemberApply(dealUnionMemberApplyReq);
        } catch (Exception e) {
            return Response.dbFail(log, "数据库操作");
        }
    }

    //强制踢出公会成员
    @PostMapping("/member/delete")
    public Response deleteMember(@RequestBody DeleteUnionMemberReq deleteUnionMemberReq) {
        return unionMemberRealization.deleteMember(deleteUnionMemberReq);
    }

    //获取申请入会的列表接口
    @PostMapping("/joinList")
    public Response getJoinList(@RequestBody getJoinListReq getJoinListReq) {
        try {
            return unionMemberRealization.getJoinList(getJoinListReq);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(log, "查询数据库");
        }
    }

    //获取申请转入公会的列表接口
    @PostMapping("/turnInList")
    public Response getTurnInList(@RequestBody getTurnInListReq getTurnInListReq) {
        return unionMemberRealization.getTurnInList(getTurnInListReq);
    }

    //获取期次接口
    @PostMapping("/get/terms")
    public Response getTerms(@RequestBody RequestParamBasic requestBody) {
        return unionRealization.getIncomeTerms();
    }

    //获取工会成员收入结算的列表接口
    @PostMapping("/income/member/list")
    public Response getUnionMemberIncomeList(
        @RequestBody GetUnionMemIncomeReq getUnionMemIncomeReq) {
        try {
            return unionMemberRealization.getUnionMemberIncomeList(getUnionMemIncomeReq);
        } catch (Exception e) {
            return Response.dbFail(log, "数据库操作失败");
        }
    }

    //处理工会成员结算
    @PostMapping("/income/member/deal")
    public Response dealUnionMemberIncome(
        @RequestBody DealUnionMemberIncomeReq dealUnionMemberIncomeReq) {
        return unionMemberRealization.dealUnionMemberIncome(dealUnionMemberIncomeReq);
    }

    //获取工会收入接口
    @PostMapping("/income/union/get")
    public Response getUnionIncome(@Valid @RequestBody GetUnionIncomeReq getUnionIncomeReq) {

        return unionRealization.getUnionIncome(getUnionIncomeReq);

    }


    //工会发票上传接口
    @PostMapping("/invoice/upload")
    public Response invoiceUpload(@RequestParam("file") MultipartFile file,
        @RequestParam("id") int id) {

        try {
            return unionMemberRealization.uploadUnionInvoice(file, id);
        } catch (Exception e) {
            return Response.error(ErrorCode.EXTERNAL_ERROR, "上传图片到服务器失败，请重新上传");
        }
    }

    //获取工会信息接口
    @PostMapping("/info/get")
    public Response getUnionInfo(@RequestBody GetUnionInfoReq getUnionInfoReq) {
        return unionRealization.getUnionInfo(getUnionInfoReq);

    }

    //重新修改工会信息接口
    @PostMapping("/info/reset")
    public Response resetUnionInfo(@RequestBody ResetUnionInfoReq resetUnionInfoReq) {
        return unionRealization.resetUnionInfo(resetUnionInfoReq);

    }


    //重置密码接口
    @PostMapping("/password/reset")
    public Response resetPassword(@RequestBody ResetPasswordReq resetPasswordReq) {
        try {
            return unionRealization.resetPassword(resetPasswordReq);
        } catch (Exception e) {
            return Response.error(ErrorCode.INTERNAL_ERROR, "服务器内部错误");
        }
    }
}
